#pragma once
#include <iostream>

namespace MyList {
	template <class T>
	struct ListNode {
		ListNode* _next;
		ListNode* _prev;
		T _data;
		// 结点的构造函数
		ListNode(const T& data)
			:_next(nullptr)
			, _prev(nullptr)
			, _data(data)
		{}
	};
 
	template <class T, class Ref, class Ptr>
	struct ListIterator {
	public:
		typedef ListNode<T> node;
		typedef ListIterator<T, Ref, Ptr> iterator;
		// 实现iterator的各种操作 前置++、--，后置++、--，==、!=
		// 对迭代器进行构造、构造函数
		ListIterator(node* n)
			:_node(n)
		{}
 
		// 未开空间的类，有无析构都无所谓
		~ListIterator() {
			_node = nullptr;
		}
		// 无内置参数，表示前置++
		iterator& operator++() {
			_node = _node->_next;
			return *this;
		}
		// 有内置参数，后置++,返回拷贝的值，不能返回引用，tmp只是一个临时变量，会被析构
		iterator operator++(int) {
			iterator tmp(_node);
			_node = _node->_next;
			return tmp;
		}
 
		iterator& operator--() {
			_node = _node->_prev;
			return *this;
		}
 
		iterator operator--(int) {
			iterator tmp(_node);
			_node = _node->_prev;
			return tmp;
		}
 
		bool operator!=(const iterator& it) {
			return _node != it._node;
		}
 
		bool operator==(const iterator& it) {
			return _node == it._node;
		}
 
		Ref operator*() {
			return _node->_data;
		}
 
		// it->->data
		Ptr operator->() {
			return &_node->_data;
		}
 
		node* _node;
	};
 
	template <class T>
	class list {
	public:
		typedef ListNode<T> node;
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;
		// ...内容
 
		// 构造和拷贝构造功能相重叠的部分
		void empty_init() {
			_head = new node(T());
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
 
		list() {
			empty_init();
		}
 
		// 拷贝构造函数
		list(const list& ls) {
			empty_init();
			for (auto e : ls) {
				push_back(e);
			}
		}
 
		~list() {
			clear();
			delete _head;
			_head = nullptr;
		}
 
		void pop_back() {
			node* tail = _head->_prev;
			node* tail_prev = tail->_prev;
			_head->_prev = tail_prev;
			tail_prev->_next = _head;
			delete tail;
			tail = nullptr;
			_size--;
			/*erase(--end());*/
		}
 
		void clear() {
			auto it = begin();
			while (it != end()) {
				it = erase(it);
			}
		}
 
		void pop_front() {
			node* cur = _head->_next;
			node* next = cur->_next;
			_head->_next = next;
			next->_prev = _head;
			delete cur;
			_size--;
			/*erase(begin());*/
		}
 
		void push_back(const T& data) {
			 
			node* newnode = new node(data);
			// 找到尾结点，新结点的prev指向尾结点，新结点的next指向头结点
			node* tail = _head->_prev;
			newnode->_prev = tail;
			tail->_next = newnode;
			newnode->_next = _head;
			_head->_prev = newnode;
			_size++;
			/*insert(end(), data);*/
		}
 
		void push_front(const T& data) {
			node* newnode = new node(data);
			node* cur = _head->_next;
			_head->_next = newnode;
			newnode->_prev = _head;
			newnode->_next = cur;
			cur->_prev = newnode;
			_size++;
			/*insert(begin(), data);*/
		}
 
		void swap(list& ls) {
			std::swap(_head, ls._head);
			std::swap(_size, ls._size);
			// 换一种写法
		}
 
		size_t size() const {
			return _size;
		}
 
		bool empty() {
			return _head == _head->_next;
		}
 
		iterator insert(iterator pos, const T& data) {
			// 在pos位置之前插入元素
			node* cur = pos._node;
			node* prev = cur->_prev;
			node* newnode = new node(data);
			cur->_prev = newnode;
			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			_size++;
			return newnode;
		}
 
		iterator erase(iterator pos) {
			node* cur = pos._node;
			node* next = cur->_next;
			node* prev = cur->_prev;
			prev->_next = next;
			next->_prev = prev;
			delete cur;
			_size--;
			return next;
		}
 
		iterator end() {
			iterator tmp(_head);
			return tmp;
		}
 
		iterator begin() {
			iterator tmp(_head->_next);
			return tmp;
		}
 
	private:
		node* _head;
		size_t _size;
	};
}